﻿using Biomedica.Graphics;
using Biomedica.Propability.StateMachine;
using KinectWindows.Math;
using Microsoft.Kinect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace NuiDicomPlayer.Nui.NavigationEngine
{
    /// <summary>
    /// Detect The State of the Left hand Motion
    /// </summary>
    public class HandMotionDetector 
    {

        // Old values
        Vector2 old_MA_head=Vector2.Zero;
        Vector2 old_MA_Spine = Vector2.Zero;
        Vector2 old_MA_Left_hand = Vector2.Zero;
        Vector2 old_MA_Right_hand = Vector2.Zero;
        Vector2 old_MA_Skeleton_Postion = Vector2.Zero;

        // Referancevalues
        Vector2 ref_MA_head = Vector2.Zero;
        Vector2 ref_MA_Spine = Vector2.Zero;
        Vector2 ref_MA_Left_hand = Vector2.Zero;
        Vector2 ref_MA_Right_hand = Vector2.Zero;
        private StateContext Context;
        public HandMotionDetector()            
        {
            
            List<State> StateCollections=new List<State>();
            // Major states
            StateCollections.Add(new State("idle"));// No action performed
            StateCollections.Add(new State("SliceMode"));// Navigate throw Slices
            StateCollections.Add(new State("BrightnessMode"));// Change Brightness
            StateCollections.Add(new State("ContrastMode"));// Change Contrast
            StateCollections.Add(new State("ZoomInMode"));// Zoom in
            StateCollections.Add(new State("ZoomOutMode"));// Zoom out
            StateCollections.Add(new State("PanMode"));// Pan Left.Right,Up,Down
            // minor states
            Context = StateMachineFactory.CreateStateMachine(StateCollections.ToArray());
            Context.initaliseTable(new String[] { "saychangeslicemode", "saychangebrightness","saychangecontrast","sayzoom","sayzoomin","sayzoomout","c","sayquit","increase"
                                                  ,"righthandmoveleft","righthandmoveright","righthandmoveup","righthandmovedown"
                                                  ,"lefthandmoveleft","lefthandmoveright","lefthandmoveup","lefthandmovedown"
            });
            /// Assigne States and Transition
            /// 
            // idel transtion

            Context.getStateMachineEngine().AddTransition("idle", "SliceMode", "saychangeslicemode");

            //Context.getStateMachineEngine().SetTranstionCallbackForState("idle", "SliceMode","saychangeslicemode", null);
            //Context.getStateMachineEngine().SetTranstionCallbackForState("idel", "BrightnessMode", "saychangebrightness", null);
            //Context.getStateMachineEngine().SetTranstionCallbackForState("idel", "ContrastMode", "saychangecontrast", null);
            //Context.getStateMachineEngine().SetTranstionCallbackForState("idel", "ZoomInMode", "sayzoominv", null);
            //Context.getStateMachineEngine().SetTranstionCallbackForState("idel", "ZoomOutMode", "sayzoomout", null);
            //Context.getStateMachineEngine().SetTranstionCallbackForState("idel", "PanMode", "saypan", null);

            Context.getStateMachineEngine().Start("idle",Context);
        }
        public  void Update(Vector2 MA_head, Vector2 MA_Spine, Vector2 MA_Left_hand, Vector2 MA_Right_hand)
           {
               Context.EnvirunmentCollection["righthandmoveleft"] = ((MA_Right_hand.X - old_MA_Right_hand.X) > 0);
               Context.EnvirunmentCollection["righthandmoveright"] = ((MA_Right_hand.X - old_MA_Right_hand.X) < 0);
               Context.EnvirunmentCollection["righthandmoveup"] = ((MA_Right_hand.Y - old_MA_Right_hand.Y) > 0);
               Context.EnvirunmentCollection["righthandmovedown"] = ((MA_Right_hand.Y - old_MA_Right_hand.Y) < 0);

               Context.EnvirunmentCollection["lefthandmoveleft"] = ((MA_Left_hand.X - old_MA_Left_hand.X) > 0);
               Context.EnvirunmentCollection["lefthandmoveright"] = ((MA_Left_hand.X - old_MA_Left_hand.X) < 0);
               Context.EnvirunmentCollection["lefthandmoveup"] = ((MA_Left_hand.Y - old_MA_Left_hand.Y) > 0);
               Context.EnvirunmentCollection["lefthandmovedown"] = ((MA_Left_hand.Y - old_MA_Left_hand.Y) < 0);

               Console.WriteLine("righthandmoveleft=>"+Context.EnvirunmentCollection["righthandmoveleft"].ToString());
               Console.WriteLine("righthandmoveright=>" + Context.EnvirunmentCollection["righthandmoveright"].ToString());
               old_MA_head = MA_head;
               old_MA_Spine = MA_Spine;
               old_MA_Left_hand = MA_Left_hand;
               old_MA_Right_hand = MA_Right_hand;
              
          }

        public void SetReferancePostions()
        {
            ref_MA_head = old_MA_head;
            ref_MA_Spine = old_MA_Spine;
            ref_MA_Left_hand = old_MA_Left_hand;
            ref_MA_Right_hand = old_MA_Right_hand;

        }




        public StateContext GetContext()
        {
            return Context;
        }
    }
   
   

}
